package com.inventory.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.util.AbDateUtil;
import com.inventory.R;
import com.inventory.global.Constant;
import com.inventory.global.MyApplication;

public class SellDataQueryActivity extends AbActivity{
	private MyApplication application;
	private TextView time1EditText = null;
	private	TextView time2EditText = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(mInflater.inflate(R.layout.sell_data_query, null));
        setTitleText(R.string.saleDataQueryTitle);
        setLogo(R.drawable.button_selector_logo);
        application = (MyApplication)abApplication;
        
        time1EditText = (TextView)this.findViewById(R.id.time1);
        time2EditText = (TextView)this.findViewById(R.id.time2);
        
        String currentDate = AbDateUtil.getCurrentDateByFormat(AbDateUtil.dateFormatYMD);
        time1EditText.setText(currentDate);
        time2EditText.setText(currentDate);
        if(Constant.TEST){
          time1EditText.setText("2012-01-01");
          time2EditText.setText("2013-02-31");
        }
        
        time1EditText.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 Intent intent = new Intent();
				 intent.setClass(SellDataQueryActivity.this, DatePickerActivity.class);
				 startActivityForResult(intent, 0);
			}
		});
        
       time2EditText.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 Intent intent = new Intent();
				 intent.setClass(SellDataQueryActivity.this, DatePickerActivity.class);
				 startActivityForResult(intent, 1);
			}
		});
       
       logoView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
        
       
        
        Button queryBtn = (Button)findViewById(R.id.queryBtn);  
        
        queryBtn.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				final String time1Str = time1EditText.getText().toString();
				final String time2Str = time2EditText.getText().toString();
				Intent intent = new Intent();
				intent.putExtra("TIME1STR", time1Str);
				intent.putExtra("TIME2STR", time2Str);
				intent.setClass(SellDataQueryActivity.this, SellDataQueryListActivity.class);
				startActivity(intent);
			}
        	
        });
       
    }
    
    
    
    private void initTitleRightLayout(){
    	clearRightView();
    	View rightViewMore = mInflater.inflate(R.layout.more_btn, null);
    	if(application.mUser != null){
    	    addRightView(rightViewMore);
    	}else{
    		addRightView(rightViewMore);
    	}
    	Button about = (Button)rightViewMore.findViewById(R.id.more_btn);
    	about.setOnClickListener(new View.OnClickListener(){

 			@Override
 			public void onClick(View v) {
 				Intent intent = new Intent(SellDataQueryActivity.this,AboutActivity.class); 
 				startActivity(intent);
 			}
         	
         });
    }

	@Override
	protected void onResume() {
		super.onResume();
		initTitleRightLayout();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0) {
			if (data != null) {
				String date = data.getStringExtra("date").trim();
				time1EditText.setText(date);
			}
		}else if(requestCode == 1){
			if (data != null) {
				String date = data.getStringExtra("date").trim();
				time2EditText.setText(date);
			}
		}
	}
	
	
}


